package com.eastfair.venueservice.service.impl;

import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.CheckAndAcceptanceMapper;
import com.eastfair.venueservice.entity.CheckAndAcceptance;
import com.eastfair.venueservice.entity.Drawing;
import com.eastfair.venueservice.service.CheckAndAcceptanceService;
import com.eastfair.boot.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import com.eastfair.venueservice.vo.*;
import com.eastfair.venueservice.dto.*;
/**
* <p>
    * 业务实现类
    * 验收记录
    * </p>
*
* @author dqq
* @date 2022-12-28
*/
@Slf4j
@Service
//@DS("#thread.tenant")
    public class CheckAndAcceptanceServiceImpl extends SuperServiceImpl<CheckAndAcceptanceMapper, CheckAndAcceptance> implements CheckAndAcceptanceService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<CheckAndAcceptance> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<CheckAndAcceptance> list(CheckAndAcceptanceDTO modelDTO, List<Long> ids) {
        return ((CheckAndAcceptanceService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    @Override
    protected R<Boolean> handlerSave(CheckAndAcceptance model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'isEnabled', #args[0]?.isEnabled},"+
       "{'exhibitionManageId', #args[0]?.exhibitionManageId},"+
       "{'checkNo', #args[0]?.checkNo},"+
       "{'systemType', #args[0]?.systemType},"+
       "{'companyName', #args[0]?.companyName},"+
       "{'applicationName', #args[0]?.applicationName},"+
       "{'phone', #args[0]?.phone},"+
       "{'orgName', #args[0]?.orgName},"+
       "{'compensate', #args[0]?.compensate},"+
       "{'finishStatus', #args[0]?.finishStatus},"+
       "{'dealResult', #args[0]?.dealResult},"+
        "{'id', #args[1]}"+
        "}"))
    public List<CheckAndAcceptance> listOfCache(CheckAndAcceptanceDTO modelDTO, List<Long> ids) {
       //查询
       QueryWrapper<CheckAndAcceptance> queryWrapper = new QueryWrapper<>();
       if (modelDTO != null) {
                    queryWrapper.lambda().eq(modelDTO.getExhibitionManageId() != null,CheckAndAcceptance::getExhibitionManageId,modelDTO.getExhibitionManageId());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getCheckNo()),CheckAndAcceptance::getCheckNo,modelDTO.getCheckNo());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSystemType()),CheckAndAcceptance::getSystemType,modelDTO.getSystemType());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getCompanyName()),CheckAndAcceptance::getCompanyName,modelDTO.getCompanyName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getApplicationName()),CheckAndAcceptance::getApplicationName,modelDTO.getApplicationName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getPhone()),CheckAndAcceptance::getPhone,modelDTO.getPhone());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getOrgName()),CheckAndAcceptance::getOrgName,modelDTO.getOrgName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getCompensate()),CheckAndAcceptance::getCompensate,modelDTO.getCompensate());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getFinishStatus()),CheckAndAcceptance::getFinishStatus,modelDTO.getFinishStatus());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getDealResult()),CheckAndAcceptance::getDealResult,modelDTO.getDealResult());
            }
            if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(CheckAndAcceptance::getId, ids);
            }
            queryWrapper.lambda().eq(CheckAndAcceptance::getIsDeleted, BusinessConstant.DELETE_NO);
            List<CheckAndAcceptance> modelList = list(queryWrapper);
            return modelList;
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public CheckAndAcceptanceVO doToVo(CheckAndAcceptance model) {
        CheckAndAcceptanceVO modelVo = new CheckAndAcceptanceVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<CheckAndAcceptanceVO> doToVos(List<CheckAndAcceptance> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<CheckAndAcceptanceVO> result = new ArrayList<>();
        for (CheckAndAcceptance model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
